// dp[i]表示金额i所需的最小硬币数，不存在为-1
// dp[i] = 1 + min(dp[i-coins[1]], dp[i-coins[2]], ...)
class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        vector<int> dp(amount + 1);
        dp[0] = 0;
        for (int i = 1; i < dp.size(); ++i) {  
            dp[i] = amount + 1;                 
        }        

        for (int i = 0; i < dp.size(); ++i) {
            for (int j = 0; j < coins.size(); ++j) {
                if (i < coins[j]) {
                    continue;
                }
                dp[i] = min(dp[i], dp[i - coins[j]] + 1);
            }
        }
        return dp[amount] == amount + 1 ? -1 : dp[amount];
    }
};